import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
        hasLogin: false,
        is_agreement:false,
        userInfo: {},
        forcedLogin: false,
        userName: "",
        userId: '',
        token: 'token',
        pointId: '',
        tabbarData:{},
        posterImage:'',
    },
    getters: {
        posterImage:function (state) {
            if(uni.getStorageSync('posterImage')) state.posterImage = uni.getStorageSync('posterImage')
            return state.posterImage;
        },
        token:function(state){
            if(uni.getStorageSync('token')) state.posterImage = uni.getStorageSync('token')
            return state.token;
        },
        hasLogin(state){
            if(uni.getStorageSync('hasLogin')) state.hasLogin = uni.getStorageSync('hasLogin')
            return state.hasLogin;
        },
        userInfo:(state)=>{
            if(uni.getStorageSync('userInfo')) state.userInfo = uni.getStorageSync('userInfo')
            return state.userInfo
        },
        is_agreement:(state,type)=>{
            if(uni.getStorageSync('is_agreement')) state.is_agreement = uni.getStorageSync('is_agreement')
            return state.is_agreement
        },
    },
    mutations: {
        set: (state,objs)=>{
            var key = Object.keys(objs)[0]
            state[key] = objs[key]
            uni.setStorageSync(key,objs[key])
        },
        get: (state,key)=>{
            if(uni.getStorageSync(key)) state[key] = uni.getStorageSync(key)
            return state[key];
        },
        login: (state, data)=>{
            state.hasLogin = true
            state.token = data.accesstoken
            state.userInfo = data || {}
            uni.setStorageSync('hasLogin',true)
            uni.setStorageSync('token',data.accesstoken)
            uni.setStorageSync('userInfo',data)
        },
        logout: (state)=>{
            state.hasLogin = false
            state.token = ''
            state.userInfo = {}
            //uni.removeStorageSync('storage_key');
            uni.clearStorageSync()
        }
    }
})
export default store